﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Threading;
using System.Xml.Linq;
using System.Collections.Generic;
using CSM.Controllers;
using CSM.Models;
using System.Text;

namespace CSM.Views.Admin
{
    public partial class AdminItemsDefine : System.Web.UI.Page
    {
        CustomDataContext db = new CustomDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                    
                    //查询当前操作员的店铺权限及客户回访等级权限。
                    string MyUserId = HttpContext.Current.Session[SystemVars.sysSessionUserId].ToString();
                    var RightList = from a in db.TCsm_JS_User
                                    where a.UserID == MyUserId
                                    select new
                                    {
                                        a.ShopRightID

                                    };
                    if (RightList.Count() > 0)
                    {

                        string MyShopRightIdList = RightList.Single().ShopRightID;
                        if (!string.IsNullOrEmpty(MyShopRightIdList))
                        {
                            var DropList_ShopRight = MyShopRightIdList.Split(',');
                            foreach (var MyItems in DropList_ShopRight)
                            {
                                var MyShopName = from a in db.TCrm_JC_Parameter
                                                 where a.ID == Convert.ToInt32(MyItems)
                                                 select a;
                                if (MyShopName.Count() == 1)
                                {
                                    var AddItem = MyShopName.Single();
                                    ListItem InsertItem = new ListItem(AddItem.ShopName, AddItem.ShopID);
                                    //填充店铺列表
                                    DropDownList_ShopId.Items.Add(InsertItem);
                                }

                            }

                        }
                    }
                    CurrentShop.Value = DropDownList_ShopId.SelectedValue;
                    //对可编辑的类需要限制
                    BindMyData(0);
                    int Totals = TotalPageAmount();
                    int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);
                    Label_Total.Text = "共" + pages.ToString();
                    Label_CurrenPageAndTo.Text = "页，当前第1" + " 页，转到";
                    EditFlag.Value = "add";
                    
            }
        }
        #region 绑定数据及翻页
        private void BindMyData(int PageIndex)
        {
            string shopid = CurrentShop.Value;
            int skipAccount = PageIndex * 10;
            var MyQuery = (from a in db.TCrm_Sys_Items join b in db.TCrm_Sys_ItemsClass on a.ClassID equals b.ClassID
                           where a.ClassID=="khdj" &&a.Shopid==shopid
                           select new
                           {
                               a.ItemID,
                               a.ItemName,
                               a.MaxValue,
                               a.MinValue,
                               a.DefaultValue,
                               b.ClassName
                           }).Skip(skipAccount).Take(10);
            GridView_Items.DataSource = MyQuery;
            GridView_Items.DataBind();
            var DayPeriod = from a in db.TCrm_Sys_Items where a.ItemID == "lssj" && a.Shopid == CurrentShop.Value select a;
            var MoneyPeroid = from a in db.TCrm_Sys_Items where a.ItemID == "je" && a.Shopid == CurrentShop.Value select a;
            TextBox_Day.Text = DayPeriod.Count()==1?DayPeriod.Single().DefaultValue:"";
            TextBox_Money.Text = MoneyPeroid.Count()==1?MoneyPeroid.Single().DefaultValue:"";

        }

        //查询出当前记录信息的总条数。
        private int TotalPageAmount()
        {
            string shopid = CurrentShop.Value;
            int MyQuery = 0;
            MyQuery = (from a in db.TCrm_Sys_Items where a.ClassID == "khdj" && a.Shopid == shopid select a).Count();
            return MyQuery;
        }


        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {


            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            int Totals = TotalPageAmount();
            int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);
            Label_Total.Text = "共" + pages.ToString();
            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "first":

                        pageIndx = 0;

                        break;
                    case "gotopage":
                        if (TextBox_PageIndex.Text == "")
                        { pageIndx = 0; }
                        else
                        {
                            int xx = Convert.ToInt32(TextBox_PageIndex.Text);
                            if (xx > pages)
                            { pageIndx = pages - 1; }

                            else
                            { pageIndx = xx - 1; }


                        }
                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";
            BindMyData(pageIndx);

        }

        #endregion
        //取消的按钮
        protected void Button_Cancel_Command(object sender, CommandEventArgs e)
        {
            EditFlag.Value = "0";
            Panel_Edit.Visible = false;
            Panel_Gridlist.Visible = true;
            if (EditFlag.Value == "edit")
            {
                TextBox_ItemsId.Enabled = true;

            }

        }
        //增加按钮的事件
        protected void Button_Add_Command(object sender, CommandEventArgs e)
        {
            Panel_Gridlist.Visible = false;
            TextBox_ItemsId.Text = "";
            TextBox_ItemsName.Text = "";
            TextBox_Default.Text = "";
            TextBox_Max.Text = "";
            TextBox_Min.Text = "";
            Panel_Edit.Visible = true;
            TextBox_ItemsId.Enabled = true;
            EditFlag.Value = "add";
            ScriptManager.RegisterStartupScript((System.Web.UI.Page)HttpContext.Current.CurrentHandler, typeof(System.Web.UI.Page), "addtext", "<script>ShowThickBox('增加','Panel_Edit');</script>", false);
        }
        //保存的按钮
        protected void Button_Save_Command(object sender, CommandEventArgs e)
        {
            string shopid = CurrentShop.Value;
            if (EditFlag.Value == "add")
            {
                
                string itemid=TextBox_ItemsId.Text;
                var query = from a in db.TCrm_Sys_Items where a.ClassID == "khdj" && a.ItemID==itemid &&a.Shopid==shopid select a;
                if(query.Count()!=0)
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('ID重复，请更改ID');</script>");
                    
                    ScriptManager.RegisterStartupScript((System.Web.UI.Page)HttpContext.Current.CurrentHandler, typeof(System.Web.UI.Page), "addtext", "<script>ShowThickBox('编辑','Panel_Edit');</script>", false);
                    return;
                }
                TCrm_Sys_Items myrow = new TCrm_Sys_Items();
                myrow.ItemID = itemid;
                myrow.ItemName = TextBox_ItemsName.Text;
                myrow.DefaultValue = TextBox_Default.Text;
                myrow.MaxValue = TextBox_Max.Text;
                myrow.MinValue = TextBox_Min.Text;
                myrow.Shopid = shopid;
                myrow.ClassID ="khdj";
                db.TCrm_Sys_Items.InsertOnSubmit(myrow);


                try
                {
                    db.SubmitChanges();
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('增加成功'); </script>");
                    EditFlag.Value = "0";
                    Panel_Edit.Visible = false;
                    Panel_Gridlist.Visible = true;
                    BindMyData(0);
                }
                catch
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('增加失败'); </script>");
                }


            }
            else if (EditFlag.Value == "edit")
            {
                string ItemsId = TextBox_ItemsId.Text;
                var myEditQuery = (from a in db.TCrm_Sys_Items where a.ItemID == ItemsId select a).Single();

                myEditQuery.ItemID = TextBox_ItemsId.Text;
                myEditQuery.ItemName = TextBox_ItemsName.Text;
                myEditQuery.DefaultValue = TextBox_Default.Text;
                myEditQuery.MaxValue = TextBox_Max.Text;
                myEditQuery.Shopid = shopid;
                myEditQuery.MinValue = TextBox_Min.Text;
                myEditQuery.ClassID ="khdj";

                try
                {
                    db.SubmitChanges();
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('编辑成功'); </script>");
                    EditFlag.Value = "0";
                    Panel_Edit.Visible = false;
                    Panel_Gridlist.Visible = true;
                    TextBox_ItemsId.Enabled = true;
                    BindMyData(0);

                }
                catch
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('编辑失败');</script>");
                }

            }

        }

        protected void Button_Edit_Command(object sender, CommandEventArgs e)
        {
            string Itemid = e.CommandArgument.ToString();
            var myEditQuery = (from a in db.TCrm_Sys_Items
                               where a.ItemID == Itemid
                               select new
                               {
                                   a.ClassID,
                                   a.DefaultValue,
                                   a.ItemID,
                                   a.ItemName,
                                   a.MaxValue,
                                   a.MinValue
                               }
                               ).Single();
            TextBox_ItemsId.Text = myEditQuery.ItemID;
            TextBox_ItemsName.Text = myEditQuery.ItemName;
            TextBox_Default.Text = myEditQuery.DefaultValue;
            TextBox_Max.Text = myEditQuery.MaxValue;
            TextBox_Min.Text = myEditQuery.MinValue;
            TextBox_ItemsId.Enabled = false;
            EditFlag.Value = "edit";
            Panel_Edit.Visible = true;
            Panel_Gridlist.Visible = false;
            ScriptManager.RegisterStartupScript((System.Web.UI.Page)HttpContext.Current.CurrentHandler, typeof(System.Web.UI.Page), "addtext", "<script>ShowThickBox('编辑','Panel_Edit');</script>", false);
        }

        protected void Button_Level_Click(object sender, EventArgs e)
        {
            string shopid = CurrentShop.Value;
            var DayPeriod = from a in db.TCrm_Sys_Items where a.ItemID == "lssj" &&a.Shopid==shopid select a;
            var MoneyPeroid = from a in db.TCrm_Sys_Items where a.ItemID == "je" && a.Shopid == shopid select a;
            DayPeriod.Single().DefaultValue = TextBox_Day.Text;
            MoneyPeroid.Single().DefaultValue = TextBox_Money.Text;
            db.SubmitChanges();
        }

        protected void DropDownList_ShopId_SelectedIndexChanged(object sender, EventArgs e)
        {
            CurrentShop.Value = DropDownList_ShopId.SelectedValue;
            //对可编辑的类需要限制
            BindMyData(0);
            int Totals = TotalPageAmount();
            int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);
            Label_Total.Text = "共" + pages.ToString();
            Label_CurrenPageAndTo.Text = "页，当前第1" + " 页，转到";
            EditFlag.Value = "add";
        }
    }
}
